<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="消耗品名称" prop="consumableName">
        <el-input
          v-model="queryParams.consumableName"
          placeholder="请输入消耗品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="消耗品规格型号" prop="consumableSize">
        <el-input
          v-model="queryParams.consumableSize"
          placeholder="请输入消耗品规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="类型" prop="consumableType">
        <el-select v-model="queryParams.consumableType" placeholder="请选择类型" clearable size="small">
          <el-option :label="item.dictLabel" :value="item.dictValue" :key="item.dictValue" v-for="item in attributeList" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="单位" prop="consumableUnit">
        <el-input
          v-model="queryParams.consumableUnit"
          placeholder="请输入单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库数量" prop="consumableReceiveCount">
        <el-input
          v-model="queryParams.consumableReceiveCount"
          placeholder="请输入入库数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库时间" prop="consumableReceiveTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.consumableReceiveTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="库存数量" prop="consumableStoreCount">
        <el-input
          v-model="queryParams.consumableStoreCount"
          placeholder="请输入库存数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="仓库名称" prop="consumableStore">
        <el-input
          v-model="queryParams.consumableStore"
          placeholder="请输入仓库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="consumablePerson">
        <el-input
          v-model="queryParams.consumablePerson"
          placeholder="请输入操作人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="操作人联系方式" prop="consumablePersonPhone">
        <el-input
          v-model="queryParams.consumablePersonPhone"
          placeholder="请输入操作人联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['beyond:consumable:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['beyond:consumable:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['beyond:consumable:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['beyond:consumable:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="consumableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="consumableId" /> -->
      <el-table-column label="消耗品名称" align="center" prop="consumableName" />
      <el-table-column label="消耗品规格型号" align="center" prop="consumableSize" />
      <el-table-column label="类型" align="center" prop="consumableType" :formatter="filterAttribute" />
      <el-table-column label="单位" align="center" prop="consumableUnit" :formatter="filterUnit"/>
      <el-table-column label="入库数量" align="center" prop="consumableReceiveCount" />
      <el-table-column label="入库时间" align="center" prop="consumableReceiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consumableReceiveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存数量" align="center" prop="consumableStoreCount" />
      <el-table-column label="仓库名称" align="center" prop="consumableStore" />
      <el-table-column label="操作人" align="center" prop="consumablePerson" />
      <el-table-column label="操作人联系方式" align="center" prop="consumablePersonPhone" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['beyond:consumable:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['beyond:consumable:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改消耗品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="消耗品名称" prop="consumableName">
          <el-input v-model="form.consumableName" placeholder="请输入消耗品名称" />
        </el-form-item>
        <el-form-item label="消耗品规格型号" prop="consumableSize">
          <el-input v-model="form.consumableSize" placeholder="请输入消耗品规格型号" />
        </el-form-item>
        <el-form-item label="类型" prop="consumableType">
          <el-select v-model="form.consumableType" placeholder="请选择类型">
            <el-option :label="item.dictLabel" :value="item.dictValue" :key="item.dictValue" v-for="item in attributeList" />
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="consumableUnit">
          <el-select v-model="form.consumableUnit" placeholder="请选择单位">
            <el-option :label="item.dictLabel" :value="item.dictValue" :key="item.dictValue" v-for="item in unitList" />
          </el-select>
        </el-form-item>
        <el-form-item label="入库数量" prop="consumableReceiveCount">
          <el-input v-model="form.consumableReceiveCount" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="入库时间" prop="consumableReceiveTime">
          <el-date-picker clearable size="small"
            v-model="form.consumableReceiveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="库存数量" prop="consumableStoreCount">
          <el-input v-model="form.consumableStoreCount" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="仓库名称" prop="consumableStore">
          <el-input v-model="form.consumableStore" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="操作人" prop="consumablePerson">
          <el-input v-model="form.consumablePerson" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="操作人联系方式" prop="consumablePersonPhone">
          <el-input v-model="form.consumablePersonPhone" placeholder="请输入操作人联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsumable, getConsumable, delConsumable, addConsumable, updateConsumable, exportConsumable } from "@/api/trace/consumable";

export default {
  name: "Consumable",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 消耗品属性列表
      attributeList: [],
      // 单位列表
      unitList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消耗品信息表格数据
      consumableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consumableName: null,
        consumableSize: null,
        consumableType: null,
        consumableUnit: null,
        consumableReceiveCount: null,
        consumableReceiveTime: null,
        consumableStoreCount: null,
        consumableStore: null,
        consumablePerson: null,
        consumablePersonPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAttributeList();
    this.getUnitList();
  },
  methods: {
    /** 查询消耗品信息列表 */
    getList() {
      this.loading = true;
      listConsumable(this.queryParams).then(response => {
        this.consumableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询消耗品属性 */
    getAttributeList() {
      this.getDicts('sys_consumable_attribute').then(response => {
        this.attributeList = response.data
      })
    },
    /** 查询单位 */
    getUnitList() {
      this.getDicts('sys_unit').then(response => {
        this.unitList = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        consumableId: null,
        consumableName: null,
        consumableSize: null,
        consumableType: null,
        consumableUnit: null,
        consumableReceiveCount: null,
        consumableReceiveTime: null,
        consumableStoreCount: null,
        consumableStore: null,
        consumablePerson: null,
        consumablePersonPhone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.consumableId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消耗品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const consumableId = row.consumableId || this.ids
      getConsumable(consumableId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消耗品信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.consumableId != null) {
            updateConsumable(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsumable(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const consumableIds = row.consumableId || this.ids;
      this.$confirm('是否确认删除消耗品信息编号为"' + consumableIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delConsumable(consumableIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有消耗品信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportConsumable(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    filterAttribute(row){
      let arr = this.attributeList.filter(val=> val.dictValue == row.consumableType)
      if(arr.length>0) return arr[0].dictLabel
    },
    filterUnit(row){
      let arr = this.unitList.filter(val=> val.dictValue == row.consumableUnit)
      if(arr.length>0) return arr[0].dictLabel
    },

  }
};
</script>
